###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/CreateCategory.pm.pl -> CreateCategory module                  #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::CreateCategory;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "HashToString.pm.pl";
require "StaticPages.pm.pl";

use strict;

require "ControlPanel/Output/CreateCategory.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "CATEGORIES_CREATE");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );
  
  my ($Skins, $SkinsIndex) = $in{'DB'}->Query(
    TABLE   => "Skins",
    SORT    => "DESCRIPTION",
    BY      => "A-Z"
  );
  
  my %INPUT;
  
  if ($SL::QUERY{'FORM_SKIN'} eq "[CUSTOM]") {
    my ($StyleSheets, $StyleSheetsIndex) = $in{'DB'}->Query(
      TABLE   => "StyleSheets",
      SORT    => "DESCRIPTION",
      BY      => "A-Z"
    );
    my ($LanguageSets, $LanguageSetsIndex) = $in{'DB'}->Query(
      TABLE   => "LanguageSets",
      SORT    => "DESCRIPTION",
      BY      => "A-Z"
    );
    my ($TemplateSets, $TemplateSetsIndex) = $in{'DB'}->Query(
      TABLE   => "TemplateSets",
      SORT    => "DESCRIPTION",
      BY      => "A-Z"
    );
    
    $INPUT{'STYLESHEETS'}  = $StyleSheets;
    $INPUT{'LANGUAGESETS'} = $LanguageSets;
    $INPUT{'TEMPLATESETS'} = $TemplateSets;
  }
  
  $INPUT{'CATEGORIES'} = $Categories;
  $INPUT{'SKINS'}      = $Skins;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::CreateCategory->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "CATEGORIES_CREATE");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my @Fields = (
    { name => "TITLE"       , required => 1, size => 128 },
    { name => "DESCRIPTION" , required => 0, size => 512 },
    { name => "IMAGE"       , required => 0, size => 512 },
    { name => "ROOT"        , required => 1              },
    { name => "USER_ADD"    , required => 0              },
    { name => "SKIN"        , required => 0              }
  );

  my (%RECORD, %EXTRA, %INPUT, @Error);

  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  foreach my $field (@{ $GENERAL->{'CATEGORY_EXTRA'} }) {
    $EXTRA{ $field->{'ID'} } = $SL::QUERY{'FORM_EXTRA_'.$field->{'ID'}};
  }

  if ($RECORD{'ROOT'} && $RECORD{'ROOT'} ne "0") {
    my $category = $in{'DB'}->BinarySelect(
      TABLE   => "Categories",
      KEY     => $RECORD{'ROOT'}
    );
    if ($category) {
      $INPUT{'CATEGORY'} = $category;
    } else {
      push(@Error, "INVALID-ROOT");
    }
  }

  if ($RECORD{'SKIN'} eq "[CUSTOM]") {
    push(@Error, "MISSING-STYLESHEET") if ($SL::QUERY{'FORM_STYLESHEET'} eq "");
    push(@Error, "MISSING-LANGUAGESET") if ($SL::QUERY{'FORM_LANGUAGESET'} eq "");
    push(@Error, "MISSING-TEMPLATESET") if ($SL::QUERY{'FORM_TEMPLATESET'} eq "");
    
    if ($SL::QUERY{'FORM_STYLESHEET'}) {
      my $stylesheet = $in{'DB'}->BinarySelect(
        TABLE   => "StyleSheets",
        KEY     => $SL::QUERY{'FORM_STYLESHEET'}
      );
      if ($stylesheet) {
        $INPUT{'STYLESHEET'} = $stylesheet;
      } else {
        push(@Error, "INVALID-STYLESHEET");
      }
    }

    if ($SL::QUERY{'FORM_LANGUAGESET'}) {
      my $languageset = $in{'DB'}->BinarySelect(
        TABLE   => "LanguageSets",
        KEY     => $SL::QUERY{'FORM_LANGUAGESET'}
      );
      if ($languageset) {
        $INPUT{'LANGUAGESET'} = $languageset;
      } else {
        push(@Error, "INVALID-LANGUAGESET");
      }
    }

    if ($SL::QUERY{'FORM_TEMPLATESET'}) {
      my $templateset = $in{'DB'}->BinarySelect(
        TABLE   => "TemplateSets",
        KEY     => $SL::QUERY{'FORM_TEMPLATESET'}
      );
      if ($templateset) {
        $INPUT{'TEMPLATESET'} = $templateset;
      } else {
        push(@Error, "INVALID-TEMPLATESET");
      }
    }
  } elsif ($RECORD{'SKIN'}) {
    my $skin = $in{'DB'}->BinarySelect(
      TABLE   => "Skins",
      KEY     => $RECORD{'SKIN'}
    );
    if ($skin) {
      $INPUT{'SKIN'} = $skin;
    } else {
      push(@Error, "INVALID-SKIN");
    }
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Inserting data...                                                    #

  if ($RECORD{'ROOT'} eq "0") {
    $RECORD{'FULL_TITLE'} = $RECORD{'TITLE'};
  } else {
    $RECORD{'FULL_TITLE'} = $INPUT{'CATEGORY'}->{'FULL_TITLE'}." / ".$RECORD{'TITLE'};
    $RECORD{'ROOT'} = $INPUT{'CATEGORY'}->{'ROOT'}.",".$RECORD{'ROOT'};
  }

  my $HashToString = HashToString->new();

  if ($RECORD{'SKIN'} eq "[CUSTOM]") {
    my %Skin;
    $Skin{'CUSTOM'}      = 1;
    $Skin{'STYLESHEET'}  = $SL::QUERY{'FORM_STYLESHEET'};
    $Skin{'LANGUAGESET'} = $SL::QUERY{'FORM_LANGUAGESET'};
    $Skin{'TEMPLATESET'} = $SL::QUERY{'FORM_TEMPLATESET'};
    
    $RECORD{'SKIN'} = $HashToString->Write(\%Skin);
  } else {
    $RECORD{'SKIN'} = $HashToString->Write({ ID => $RECORD{'SKIN'} });
  }

  $RECORD{'EXTRA'} = $HashToString->Write(\%EXTRA);

  (
    $RECORD{'ID'} = $in{'DB'}->Insert(
      TABLE   => "Categories",
      VALUES  => \%RECORD
    )
  ) || &Error::Error("CP", MESSAGE => "Error inserting record. $in{'DB'}->{'ERROR'}");

  $RECORD{'skin'}  = eval($RECORD{'SKIN'});
  $RECORD{'extra'} = eval($RECORD{'EXTRA'});

  my $StaticPages = StaticPages->new(DB => $in{'DB'});
     $StaticPages->Update(TYPE => "Category", INPUT => \%RECORD)
       || &Error::Error("CP", MESSAGE => "Error updating static pages. $StaticPages->{'ERROR'}");

  $INPUT{'RECORD'} = \%RECORD;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::CreateCategory->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;